<template>
  <div>
    <el-dialog
      :title="form.uid ? '分类编辑' : '分类添加'"
      :visible.sync="buffer.Bufferfrom"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="商品分类" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in mylist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 图片 -->
        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :auto-upload="false"
            :on-change="imgchange"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 图片 -->
        <el-form-item label="菜单状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="buffer.Bufferfrom = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["buffer", "mylist"],
  name: "",
  components: {},
  data() {
    return {
      form: {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      imageUrl: "",
      formLabelWidth: "120px",
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  created() {},
  watch: {
    buffer: {
      handler(newV, oldV) {
        if (newV.Bufferfrom == false) {
          this.form = {
            pid: 0,
            catename: "",
            img: "",
            status: 1,
          };
        }
      },
      deep: true,
    },
  },
  mounted() {},
  methods: {
    submit() {
      if (this.form.id) {
        this.$api
          .cateEdit(this.form)
          .then((res) => {
            console.log(res);
            this.buffer.Bufferfrom = false;
          })
          .catch((err) => console.log(err));
      } else {
        this.$api
          .cateAdd(this.form)
          .then((res) => {
            console.log(res);
            this.buffer.Bufferfrom = false;
          })
          .catch((err) => console.log(err));
      }
    },
    imgchange(file) {
      let { name, size } = file;
      if (size > 1024 * 1024 * 2) {
        this.$message({
          message: "大小超过2M！",
          type: "warning",
        });
      }
      let allowSuffix = ["jpeg", "jpg", "png", "gif"];
      if (!allowSuffix.includes(name.split(".").pop())) {
        this.$message({
          message: "非法文件！",
          type: "warning",
        });
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
  },
};
</script>
<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
